package student.object.clone;

public class CatTest {


    public static void main(String[] args) throws CloneNotSupportedException {
        Cat cat = new Cat();
        cat.setAge("10");
        Dog dog = new Dog();
        dog.setName("旺旺");
        cat.setDog(dog);

        Cat catClone = (Cat) cat.clone();
        System.out.println("cat == catClone => " + (cat == catClone));
        System.out.println("cat.hashCode => " + cat.hashCode());
        System.out.println("catClone.hashCode => " + catClone.hashCode());
        System.out.println("cat age => " + catClone.getAge());

        System.out.println("================================ 拷贝对象重新设置值后 =================================");

        cat.setAge("20");
        cat.getDog().setName("旺旺clone");

        System.out.println("cat.class == catClone.class => " + (cat.getClass() == catClone.getClass()));

        System.out.println("cat age => " + cat.getAge());
        System.out.println("catClone age => " + catClone.getAge());
        System.out.println("cat dog age => " + cat.getDog().getName());
        System.out.println("catClone dog age => " + catClone.getDog().getName());

        /*
           结论：clone是浅拷贝，普通属性（基础数据类型）拷贝的是值，引用类型属性拷贝的引用。
           所以 原始对象和拷贝对象 普通属性互不干扰，引用类型属性指向的同一个内存地址，互相共享
         */
    }
}
